package com.zxy.service;

import cn.echo.threadlocal.LocalUserUtils;
import cn.hutool.core.date.DateUtil;
import com.zxy.dao.OoItemDao;
import com.zxy.dao.OoMasterDao;
import com.zxy.model.OoItem;
import com.zxy.model.OoMaster;
import com.zxy.model.OoMasterQuery;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OoMasterService {
    @Resource
    private OoMasterDao ooMasterDao;
    @Resource
    private OoItemService ooItemService;
    @Resource
    private OoItemDao ooItemDao;

    public int insert(OoMaster model) {
        String ooId = "IO" + DateUtil.format(DateUtil.date(), "yyyyMMddHHmmssSSS");
        model.setId(ooId);
        String nickName = LocalUserUtils.get().getNickName();
        model.setOPerson(nickName);
        model.setOoDate(LocalDateTime.now());
        OoMasterService ooMasterService = (OoMasterService) AopContext.currentProxy();
        ooMasterService.addModel(model);
        return 1;
    }

    @Transactional
    public void addModel(OoMaster model) {
        ooMasterDao.insert(model);
        for (OoItem item : model.getItems()) {
            item.setOoId(model.getId());
            ooItemService.insert(item);
        }
    }

    public List<OoMaster> select(OoMasterQuery query) {
        return ooMasterDao.select(query);
    }

    public List<OoItem> items(String id) {
        return ooItemDao.selectByMasterId(id);
    }
}
